home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 10 / FM Towns Free Software Collection 10.iso / ms_dos / tool / fwcp / src / input.c < prev    next >
Text File  |  1995-03-08  |  5KB  |  246 lines

  1. #include    <stdio.h>
  2. #include    <stdlib.h>
  3. #include    <string.h>
  4. #include    <ctype.h>
  5. #include    <malloc.h>
  6.  
  7. #ifdef  MSDOS
  8. #include    <jctype.h>
  9. #endif
  10.  
  11. #include    "defs.h"
  12. #include    "key.h"
  13.  
  14. int    GETCH();
  15. void    UNGETCH(int ch);
  16. void    LOCATE(int x,int y);
  17. void    REPCHR(int ch,int n);
  18. void    BEEP(void);
  19. void    BAKSPC(int n);
  20. void    PUTC(int ch);
  21. void    PUTS(char *str);
  22. void    FLUSH(void);
  23.  
  24. #define    HISMAX    16
  25.  
  26. static    char    *hisv[HISMAX + 1];
  27. static    char    undo_buf[128];
  28.  
  29. int     iskan(char *p)
  30. {
  31.     if ( iskanji(*p) && iskanji2(*(p+1)) )
  32.         return TRUE;
  33.     else
  34.         return FALSE;
  35. }
  36. int     kan_pos(char *p, int n)
  37. {
  38.     int     i;
  39.  
  40.     for ( i = n ; i > 0 ; ) {
  41.         if ( *p == '\0' ) {
  42.             return (n - i);
  43.         } else if ( iskan(p) ) {
  44.             i -= 2;
  45.             p += 2;
  46.         } else {
  47.             i--;
  48.             p++;
  49.         }
  50.     }
  51.     return (n + i);
  52. }
  53. char    *stralloc(char *str)
  54. {
  55.     char *p;
  56.  
  57.     if ( (p = (char *)malloc(strlen(str) + 1)) != NULL )
  58.     strcpy(p, str);
  59.     return p;
  60. }
  61. int    input(int x, int y, int cols, int max, int no, char *buf)
  62. {
  63.     int  i, n, ch;
  64.     char *p;
  65.     static int len = 0;
  66.     static int pos = 0;
  67.     static int top = 0;
  68.  
  69.     for ( ; ; ) {
  70.     while ( top > 0 && (pos - top) < 10 )
  71.         top = kan_pos(buf, top - 10);
  72.  
  73.     while ( (pos - top) >= (cols - 5) )
  74.         top = kan_pos(buf, top + 10);
  75.  
  76.     p = buf + top;
  77.     n = 0;
  78.     i = cols;
  79.     LOCATE(x, y);
  80.  
  81.     if ( top > 0 ) {
  82.         BOLDCOL();
  83.         PUTANK('<');
  84.         NOMCOL();
  85.         n++;
  86.     }
  87.  
  88.     if ( (len - top) > cols )
  89.         i--;
  90.  
  91.     while ( (n + top) < len && n < i ) {
  92.         if ( iskan(p) ) {
  93.         if ( (n + 2) > i )
  94.             break;
  95.         PUTKAN((p[0] << 8) | (p[1] & 0xFF));
  96.         n += 2;
  97.         p += 2;
  98.         } else {
  99.         PUTANK(*p);
  100.         n += 1;
  101.         p += 1;
  102.         }
  103.     }
  104.  
  105.     if ( (n + top) < len ) {
  106.         BOLDCOL();
  107.         PUTANK('>');
  108.         NOMCOL();
  109.         n++;
  110.     }
  111.  
  112.     REPCHR(' ', cols - n);
  113.     LOCATE(x + (pos - top), y);
  114.     FLUSH();
  115.  
  116.     while ( KBHIT() == 0 )
  117.         wait_loop();
  118.  
  119.     ch = GETCH();
  120.  
  121.     if ( ch == K_END_LINE ) {
  122.         buf[len] = '\0';
  123.         len = pos = top = 0;
  124.  
  125.         if ( buf[0] != '\0' ) {
  126.         if ( hisv[0] != NULL && strcmp(hisv[0], buf) == 0 )
  127.             break;
  128.         if ( hisv[HISMAX - 1] != NULL )
  129.             free(hisv[HISMAX - 1]);
  130.         for ( n = (HISMAX - 1) ; n > 0 ; n-- )
  131.             hisv[n] = hisv[n - 1];
  132.         hisv[0] = stralloc(buf);
  133.         }
  134.         break;
  135.  
  136.     } else if ( ch == K_BACK_SPC && pos > 0 ) {
  137.         pos = kan_pos(buf, pos - 1);
  138.         p = &(buf[pos]);
  139.         n = (iskan(p) ? 2 : 1);
  140.         len -= n;
  141.         memcpy(p, p + n, len - pos);
  142.  
  143.     } else if ( ch == K_DEL_CHAR && pos < len ) {
  144.             p = &(buf[pos]);
  145.             n = (iskan(p) ? 2 : 1);
  146.             len -= n;
  147.             memcpy(p, p + n, len - pos);
  148.  
  149.     } else if ( ch == K_LEFT_CUR && pos < len ) {
  150.             p = &(buf[pos]);
  151.             n = (iskan(p) ? 2 : 1);
  152.             pos += n;
  153.  
  154.     } else if ( ch == K_RIGHT_CUR && pos > 0 ) {
  155.             pos = kan_pos(buf, pos - 1);
  156.  
  157.     } else if ( ch == K_CUT_BUFF ) {
  158.         strncpy(undo_buf, buf, len);
  159.         undo_buf[len] = '\0';
  160.  
  161.     } else if ( ch == K_DEL_LINE ) {
  162.         strncpy(undo_buf, buf, len);
  163.         undo_buf[len] = '\0';
  164.         pos = len = 0;
  165.  
  166.     } else if ( ch == K_UNDO_LINE ) {
  167.         pos = len = strlen(undo_buf);
  168.         memcpy(buf, undo_buf, len);
  169.  
  170.     } else if ( ch == K_HIS_LINE ) {
  171.         while ( pos < max && buf[pos] != '\0' )
  172.         pos++;
  173.         len = pos;
  174.  
  175.     } else if ( ch == K_GETS_WIND ) {
  176.         if ( (pos += get_wind(no, TRUE, max - pos, buf + pos)) > len )
  177.         len = pos;
  178.  
  179.     } else if ( ch == K_GETS_SRC ) {
  180.         if ( (pos += get_wind(0, FALSE, max - pos, buf + pos)) > len )
  181.         len = pos;
  182.  
  183.     } else if ( ch == K_GETS_DIS ) {
  184.         if ( (pos += get_wind(1, FALSE, max - pos, buf + pos)) > len )
  185.         len = pos;
  186.  
  187.     } else if ( ch == K_HISTORY ) {
  188.         for ( n = 0 ; n < HISMAX ; n++ ) {
  189.         if ( hisv[n] == NULL )
  190.             break;
  191.         }
  192.         if ( n > 0 ) {
  193.         SAVESCREEN();
  194.         n = menu(x + 5, y - 2 - n, 0, hisv);
  195.         LOADSCREEN();
  196.         FLUSH();
  197.         if ( n >= 0 ) {
  198.             pos = len = strlen(hisv[n]);
  199.             memcpy(buf, hisv[n], len);
  200.         }
  201.         }
  202.  
  203.     } else if ( (ch & 0xFF00) == 0 && ch >= ' ' && ch != 0x7F ) {
  204.         if ( iskanji(ch) ) {
  205.         i = GETCH();
  206.         if ( iskanji2(i) ) {
  207.             ch = (ch << 8) | i;
  208.             n = 2;
  209.             if ( ch == 0x8140 )        /* SPC */
  210.             ch = 0x2020;
  211.         } else {
  212.             UNGETCH(i);
  213.             n = 1;
  214.         }
  215.         } else {
  216.         n = 1;
  217.         }
  218.  
  219.         if ( (len + n) > max ) {
  220.         BEEP();
  221.         continue;
  222.         }
  223.  
  224.         if ( pos < len ) {
  225.         p = &(buf[len + (n - 1)]);
  226.         for ( i = len - pos ; i > 0 ; i--,p-- )
  227.             *p = *(p - n);
  228.         }
  229.  
  230.         if ( n == 1 ) {
  231.         buf[pos++] = ch;
  232.         } else {
  233.         buf[pos++] = ch >> 8;
  234.         buf[pos++] = ch;
  235.         }
  236.  
  237.         len += n;
  238.  
  239.     } else if ( (ch & 0xFF00) != 0 || ch < ' ' ) {
  240.         break;
  241.     }
  242.     }
  243.  
  244.     return ch;
  245. }
  246.